import store from "@/store";
import {useDispatch} from "react-redux";
import {useMemo} from "react";
import {renderSlider} from "@/routes";
import {useLocation,matchPath} from "react-router-dom";

export const useAppDispatch = ()=>{
    type TDispatch = typeof store.dispatch;
    return useDispatch<TDispatch>();
}

export const useAppTitleInfo = ()=>{
    const {pathname} = useLocation();
    return  useMemo(function():TTitleInfo{
        const info = renderSlider().find((v:any)=>v.path === "/"+pathname.split("/")[1]);
        const title = info.label as string;// 一级标题
        let subTitle;
        // /hospital/show/622574cc36a9ba1be763dad8
        if(info.children) {
            // console.log(pathname);// /hospital/show/622574cc36a9ba1be763dad8
            // 浏览器中的地址： /hospital/show/622574cc36a9ba1be763dad8
            // 路由配置中的地址：/hospital/show/:id
            // console.log(matchPath("/hospital/show/:id","/hospital/show/622574cc36a9ba1be763dad8"))
            // console.log(matchPath("/hospital/show/:id/:a","/hospital/show/622574cc36a9ba1be763dad8"))



            const subInfo =info.children.find((v:any)=>matchPath(v.path,pathname));
            if(subInfo) subTitle = subInfo.label;
            // subTitle = info.children.find((v:any)=>v.path === pathname).label;
        }

        return  {
            title,
            subTitle
        };
    },[pathname]);
}